// grass/pages/topicSelect/topicSelect.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        name: "",
        topicList: [],
        sercherStorage: [],
        //历史记录
        status: 0
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        if (options.status) {
            that.setData({
                status: options.status
            });
        }
        that.openLocationsercher();
        that.queryTopics();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        let that = this;
        that.openLocationsercher();
        that.queryTopics();
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    recordValue: function(e) {
        let key = e.currentTarget.dataset.name;
        let value = e.detail.value.replace(/\s+/g, "");
        this.setData({
            [key]: value
        });
    },
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        let that = this;
        that.openLocationsercher();
        that.queryTopics();
        wx.stopPullDownRefresh();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    //全局基础配置信息
    getBase: function(openid) {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    color_header: res.extra.color_header,
                    color_text: res.extra.color_text,
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    style: res.extra.style
                });
            }
        });
    },
    queryTopics: function() {
        var that = this;
        common.request({
            endpoint: "Plantgrass/manage/grass_topic",
            method: "post",
            data: {
                keyword: that.data.name
            },
            success: res => {
                that.setData({
                    topicList: res.extra
                });
            }
        });
    },
    chooseTopic: function(e) {
        let that = this;
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        var id = e.currentTarget.dataset.item.id;
        var title = e.currentTarget.dataset.item.title;
        let sercherStorage = that.data.sercherStorage;
        let topicList = [ e.currentTarget.dataset.item ];
        // if (prevPage.route=='grass/pages/grass_index/grass_index'){
        //   prevPage.setData({
        //     page:1,
        //     chooseTopic: e.currentTarget.dataset.item.id,
        //     topicList: topicList
        //   })
        //   prevPage.dealTopList();
        //   prevPage.getNewGrass();
        // }else{
        //   prevPage.setData({
        //     topic_name: e.currentTarget.dataset.item.title,
        //     topic_id: e.currentTarget.dataset.item.id,
        //   })
        // }
                if (sercherStorage.length >= 10) {
            sercherStorage.splice(9);
        }
        that.data.sercherStorage.map((x, i) => {
            if (x.id == id) {
                sercherStorage.splice(i, 1);
            }
        });
        sercherStorage.unshift({
            id: id,
            title: title
        });
        wx.setStorageSync("recentData", sercherStorage);
        if (that.data.status == 1) {
            prevPage.setData({
                topic_name: e.currentTarget.dataset.item.title,
                topic_id: e.currentTarget.dataset.item.id
            });
            wx: wx.navigateBack({
                //返回上一个页面
                delta: 1
            });
        } else {
            wx.navigateTo({
                url: "/grass/pages/grassTopics/grassTopics?info=" + JSON.stringify(e.currentTarget.dataset.item)
            });
        }
    },
    //清除缓存历史
    clearSearchStorage: function() {
        let that = this;
        wx.removeStorageSync("recentData");
        that.setData({
            sercherStorage: []
        });
    },
    //打开历史记录列表
    openLocationsercher: function() {
        let that = this;
        that.setData({
            sercherStorage: wx.getStorageSync("recentData") || []
        });
    }
});